package com.luo.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luo.demo.entity.User;
import com.luo.demo.mapper.UserMapper;
import com.luo.demo.service.UserService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    /**
     * 查询所有用户
     */
    @Override
    public List<User> listAllUsers() {
        return baseMapper.selectList(null); // null 表示无查询条件
    }

    /**
     * 根据ID查询用户
     */
    @Override
    public User getUserById(Integer id) {
        return baseMapper.selectById(id);
    }

    /**
     * 添加用户
     */
    @Override
    public boolean addUser(User user) {
        return baseMapper.insert(user) > 0;
    }

    /**
     * 更新用户
     */
    @Override
    public boolean updateUser(User user) {
        return baseMapper.updateById(user) > 0;
    }

    /**
     * 根据ID删除用户
     */
    @Override
    public boolean deleteUserById(Integer id) {
        return baseMapper.deleteById(id) > 0;
    }

    /**
     * 根据用户名查询用户（自定义查询）
     */
    @Override
    public List<User> getUsersByUserName(String userName) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", userName); // 查询条件：user_name = ?
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 根据性别查询用户（自定义查询）
     */
    @Override
    public List<User> getUsersBySex(String sex) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sex", sex); // 查询条件：sex = ?
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 根据用户名获取用户对象
     * @param username
     * @return
     */
    @Override
    public User getUserByUsername(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",username);
        return baseMapper.selectOne(queryWrapper);
    }
}